package com.springfairytale.qqserver.main;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import com.springfairytale.qqserver.action.BaseAction;
import com.springfairytale.qqserver.entity.User;

public class Server {
	private InputStream in;
	private SAXBuilder saxBuilder = new SAXBuilder();
	private Document doc;

	public static void main(String[] args) throws IOException {
		new Server().serverStart();
	}

	private void serverStart() {
		loadCache();
		loadAction();
		start();
	}

	private void start() {
		ServerSocket server;
		try {
			server = new ServerSocket(8888);
			while (true) {
				Socket socket = server.accept();
				new Thread(new ClientRunnable(socket)).start();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("server stop unexceptedly!");
			System.out.println("Server restart!");
			serverStart();
		}

	}

	private void loadCache() {
		in = this.getClass().getClassLoader().getResourceAsStream("users.xml");
		try {
			doc = saxBuilder.build(in);
		} catch (JDOMException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		Element root = doc.getRootElement();
		List<Element> users = root.getChildren("user");
		for (Element e : users) {
			String uid = e.getChildText("uid");
			String password = e.getChildText("password");
			String nickname = e.getChildText("nickname");
			String pic =e.getChildText("pic");
			Integer sex = Integer.parseInt(e.getChildText("sex"));
			String birthday = e.getChildText("birthday");
			String address = e.getChildText("address");
			String signature = e.getChildText("signature");
	//		Integer state = Integer.parseInt(e.getChildText("state"));
		    
			User user = new User(uid, password, nickname, pic, sex, birthday, address, signature,0) ;
			 
			Context.usersInfo.put(uid, user);
		}
	}
	

	private void loadAction() {
		in = this.getClass().getClassLoader().getResourceAsStream("action.xml");
		try {
			doc = saxBuilder.build(in);
		} catch (JDOMException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		Element root = doc.getRootElement();
		List<Element> actions = root.getChildren("action");
		for (Element e : actions) {
			Integer actid = Integer.parseInt(e.getChildText("actionId"));
			String cls = e.getChildText("className");
			BaseAction act = null;
			try {
				act = (BaseAction) Class.forName(cls).newInstance();
			} catch (InstantiationException | IllegalAccessException
					| ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
			Context.actions.put(actid, act);
		}
	}
}
